package com.netty.helloworld;

import io.netty.bootstrap.Bootstrap;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;

/**
 * 客户端
 * @author gxkj-042
 *
 */
public class Client {

	public static void main(String[] args) throws Exception {
		EventLoopGroup group = new NioEventLoopGroup();
		//配置参数
		Bootstrap bootstrap = new Bootstrap();
		bootstrap.group(group)			//绑定线程组
		.channel(NioSocketChannel.class)//指定NIO的模式
		.handler(new ChannelInitializer<SocketChannel>() {
			@Override
			protected void initChannel(SocketChannel sc) throws Exception {
				//配置处理数据的方法
				sc.pipeline().addLast(new ClientHandler());
			}
		});
		ChannelFuture cf = bootstrap.connect("127.0.0.1",8765);
		//发送消息
		Thread.sleep(1000);
		cf.channel().writeAndFlush(Unpooled.copiedBuffer("777".getBytes()));
		cf.channel().writeAndFlush(Unpooled.copiedBuffer("888".getBytes()));
		Thread.sleep(2000);
		cf.channel().writeAndFlush(Unpooled.copiedBuffer("999".getBytes()));
		
		cf.channel().closeFuture().sync();
		group.shutdownGracefully();
	}
}
